package com.msstudio.utils;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 

import com.msstudio.midlets.MSCMIDlet;
import java.io.*;
import java.util.Date;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import com.msstudio.websocket.client.WebSocketClient;

public class HttpPoster
        implements Runnable {//HTTPͨ���ࣻ

    private volatile boolean aborting;
    private Vector requestQueue;
    private Vector listenerQueue;
    private Vector vDeleteItems;
    private final int C_SendInterval = 6;
    private String strUin;//�û����룻
    private int iSeqNo;
    private int iDBTimer;
    private int iDBLeftTimer;
    private Vector DBCommandQueue;
    private boolean bExit;
    private int iTimerCnt;
    private int iNetErrorCnt;
    Thread thread;
    private final int C_NetError_TimeCnt = 600;
    private boolean timerOn;
    private WebSocketClient webSocketClient;

    public HttpPoster() {
        webSocketClient = new WebSocketClient();
        aborting = false;
        requestQueue = new Vector();
        listenerQueue = new Vector();
        vDeleteItems = new Vector();
        iDBTimer = 0;
        iDBLeftTimer = 0;
        DBCommandQueue = new Vector();
        bExit = false;
        iTimerCnt = 0;
        iNetErrorCnt = 0;
        thread = null;
        timerOn = false;
        iSeqNo = getInitSeqNo();
        thread = new Thread(this);
        thread.start();
    }

    public String getUINbySeq(String s) {
        String s1 = null;
        for (int i = 0; i < vDeleteItems.size(); i += 2) {
            if (!s.equals((String) vDeleteItems.elementAt(i))) {
                continue;
            }
            s1 = (String) vDeleteItems.elementAt(i + 1);
            vDeleteItems.removeElementAt(i + 1);
            vDeleteItems.removeElementAt(i);
            break;
        }

        return s1;
    }

    public void startTimer() {
        timerOn = true;
    }

    public void stopTimer() {
        timerOn = false;
        DBCommandQueue.removeAllElements();
    }

    public void OnTimer() {
        if (!timerOn) {
            return;
        }
        iTimerCnt++;
        if (iDBTimer != 0) {
            iDBTimer--;
            if (iDBTimer % 10 == 0) {
                getDBCommand();
            }
        }
        if (iDBLeftTimer != 0) {
            iDBLeftTimer--;
        }
        if (++iNetErrorCnt > 600) {
            iNetErrorCnt = 0;
            MSCMIDlet.instance.onNetError(true);
        }
    }

    private void addDBCommand(String s, HttpPosterListener httpposterlistener) {
        if (iDBLeftTimer != 0) {
            DBCommandQueue.addElement(s);
            DBCommandQueue.addElement(httpposterlistener);
            iDBTimer += 10;
        } else {
            sendRequest(s, httpposterlistener);
        }
        iDBLeftTimer += 10;
    }

    private void getDBCommand() {
        if (DBCommandQueue.size() < 2) {
            return;
        } else {
            String s = (String) DBCommandQueue.elementAt(0);
            HttpPosterListener httpposterlistener = (HttpPosterListener) DBCommandQueue.elementAt(1);
            sendRequest(s, httpposterlistener);
            DBCommandQueue.removeElementAt(1);
            DBCommandQueue.removeElementAt(0);
            return;
        }
    }

    public String GetSelfUIN() {
        return strUin;
    }

    private int getInitSeqNo() {
        Date date = new Date();
        int i = (int) date.getTime() >>> 1;
        return i % 256;
    }

    public void login(String s, String s1, HttpPosterListener httpposterlistener) {
        String s2 = null;
        strUin = s;
        String s3 = MD5.toMD5(s1);
        s2 = "VER=1.0&CMD=Login&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&PS=" + s3 + "&M5=1" + ("&LG=" + MSCMIDlet.multiLang.language) + "&LC=9326B87B234E7235";
        addDBCommand(s2, httpposterlistener);
    }

    public void getMsg(HttpPosterListener httpposterlistener) {//ȡ����Ϣ��
        Object obj = null;
        for (int i = 0; i < listenerQueue.size(); i++) {
            String s = ((HttpPosterListener) listenerQueue.elementAt(i)).toString();
            if (s.equals(httpposterlistener.toString())) {
                return;
            }
        }

        String s1 = null;
        s1 = "VER=1.0&CMD=GetMsgEx&SEQ=" + iSeqNo++ + "&UIN=" + strUin;
        sendRequest(s1, httpposterlistener);//�������󴮣�
    }

    public void sendMsg(String s, String s1, HttpPosterListener httpposterlistener) {//������Ϣ����һ������Ϊ���ڶ�������Ϊ��Ϣ���ݣ��������ϢΪ����ڣ�
        String s2 = null;
        s2 = "VER=1.0&CMD=CLTMSG&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&UN=" + s + "&MG=" + s1;
        sendRequest(s2, httpposterlistener);
    }

    public void sendList(HttpPosterListener httpposterlistener) {
        String s = null;
        s = "VER=1.0&CMD=List&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&TN=160&UN=0";
        sendRequest(s, httpposterlistener);
    }

    public void getFriend(HttpPosterListener httpposterlistener) {
        String s = null;
        s = "VER=1.0&CMD=Query_Stat&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&TN=50&UN=0";
        sendRequest(s, httpposterlistener);
    }

    public void getInfor(String s, HttpPosterListener httpposterlistener) {
        String s1 = null;
        s1 = "VER=1.0&CMD=GetInfo&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&LV=2" + "&UN=" + s;
        addDBCommand(s1, httpposterlistener);
    }

    public void addFriend(String s, HttpPosterListener httpposterlistener) {//��һ�����ѣ���һ������Ϊ���ڶ�������Ϊ������
        String s1 = null;
        s1 = "VER=1.0&CMD=AddToList&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&UN=" + s;
        addDBCommand(s1, httpposterlistener);
    }

    public void ackAddFriend(String s, String s1, String s2, HttpPosterListener httpposterlistener) {//Ӧ��������ѣ���һ������Ϊ���ڶ�������Ϊ�����������Ϊ�����ĸ�����Ϊ��
        String s3 = null;
        s3 = "VER=1.0&CMD=Ack_AddToList&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&UN=" + s + "&CD=" + s1 + "&RS=" + s2;
        if (s1.equals("2")) {
            addDBCommand(s3, httpposterlistener);
        } else {
            sendRequest(s3, httpposterlistener);
        }
    }

    public void delFriend(String s, HttpPosterListener httpposterlistener) {
        String s1 = null;
        vDeleteItems.addElement(Integer.toString(iSeqNo));
        vDeleteItems.addElement(s);
        s1 = "VER=1.0&CMD=DelFromList&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&UN=" + s;
        sendRequest(s1, httpposterlistener);
    }

    public void finger(String s, String s1, String s2, HttpPosterListener httpposterlistener) {
        String s3 = null;
        s3 = "VER=1.0&CMD=Finger&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&AG=" + s + "&SX=" + s1 + "&PV=" + s2;
        sendRequest(s3, httpposterlistener);
    }

    public void changeStat(String s, HttpPosterListener httpposterlistener) {
        String s1 = null;
        s1 = "VER=1.0&CMD=Change_Stat&SEQ=" + iSeqNo++ + "&UIN=" + strUin + "&ST=" + s;
        sendRequest(s1, httpposterlistener);
    }

    public void logout(HttpPosterListener httpposterlistener) {
        if (requestQueue.size() > 0) {
            MSCMIDlet.instance.quitApp();
            return;
        } else {
            bExit = true;
            MSCMIDlet.mainForm.StopQQ();
            String s = null;
            s = "VER=1.0&CMD=Logout&SEQ=" + iSeqNo++ + "&UIN=" + strUin;
            sendRequest(s, httpposterlistener);
            return;
        }
    }

    public synchronized void sendRequest(String s, HttpPosterListener httpposterlistener) {
        if (iSeqNo == 65535) {
            iSeqNo = 1;
        }
        requestQueue.addElement(s);
        listenerQueue.addElement(httpposterlistener);
        startToSend();
    }

    public void startToSend() {
        notify();
    }

    public void run() {
        while (!aborting) {
            try {
                wait();
            } catch (InterruptedException interruptedexception) {
                System.out.println("error in httpposter-run");
            }
            if (aborting) {
                break;
            }
            if (requestQueue.size() == 0) {
                continue;
            }
            String s = (String) requestQueue.elementAt(0);
            HttpPosterListener httpposterlistener = (HttpPosterListener) listenerQueue.elementAt(0);
            synchronized  (this)
            {
                requestQueue.removeElementAt(0);
                listenerQueue.removeElementAt(0);
            }
            doSend(s, httpposterlistener);            
        }
    }

    private boolean doSend(String s, HttpPosterListener httpposterlistener) {
        HttpConnection httpconnection;
        DataInputStream datainputstream;
        DataOutputStream dataoutputstream;
        String s1;
        String s2;
        boolean flag;
        httpconnection = null;
        datainputstream = null;
        dataoutputstream = null;
        s1 = null;
        s2 = null;
        flag = false;
        boolean flag1;
        try {
            httpconnection = (HttpConnection) Connector.open(MSCMIDlet.url, 3, true);
            httpconnection.setRequestMethod("POST");
            byte abyte0[] = PkgTools.convertUnicode2UTF8Byte(s);
            httpconnection.setRequestProperty("Content-Length", "" + abyte0.length);
            dataoutputstream = httpconnection.openDataOutputStream();
            dataoutputstream.write(abyte0);
            dataoutputstream.flush();
        } catch (Exception ex)
        {
            
        }

        MSCMIDlet.instance.quitApp();
        flag1 = true;
        if (datainputstream != null) {
            try {
                datainputstream.close();
            } catch (IOException ioexception4) {
            }
        }
        if (dataoutputstream != null) {
            try {
                dataoutputstream.close();
            } catch (IOException ioexception5) {
            }
            return false;
        }
        if (httpconnection != null) {
            try {
                httpconnection.close();
            } catch (IOException ioexception6) {
            }
            return false;
        }
        int i = 0;
        byte abyte1[];
        try {
            datainputstream = httpconnection.openDataInputStream();
            i = (int) httpconnection.getLength();
            
            if (i != -1) {
                abyte1 = new byte[i];
                datainputstream.readFully(abyte1);
            } else {
                ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
                int j;
                while ((j = datainputstream.read()) != -1) {
                    bytearrayoutputstream.write(j);
                }
                abyte1 = bytearrayoutputstream.toByteArray();
                bytearrayoutputstream.close();
            }
        } catch (Exception ex)
        {
            return false;
        }
        i = abyte1.length;
        byte abyte2[] = new byte[i + 2];
        abyte2[0] = (byte) (i >> 8);
        abyte2[1] = (byte) i;
        for (int k = 0; k < i; k++) {
            abyte2[k + 2] = abyte1[k];
        }

        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte2);
        DataInputStream datainputstream1 = new DataInputStream(bytearrayinputstream);
        try {
            s1 = datainputstream1.readUTF();        
            if (s1 == null) {
                flag = true;
                s2 = "http respone null";
            }
        } catch (IOException ioexception) {
        }
        if (datainputstream != null) {
            try {
                datainputstream.close();
            } catch (IOException ioexception) {
            }
        }
        if (dataoutputstream != null) {
            try {
                dataoutputstream.close();
            } catch (IOException ioexception1) {
            }
        }
        if (httpconnection != null) {
            try {
                httpconnection.close();
            } catch (IOException ioexception2) {
            }
        }
        flag = true;
        if (datainputstream != null) {
            try {
                datainputstream.close();
            } catch (IOException ioexception7) {
            }
        }
        if (dataoutputstream != null) {
            try {
                dataoutputstream.close();
            } catch (IOException ioexception8) {
            }
        }
        if (httpconnection != null) {
            try {
                httpconnection.close();
            } catch (IOException ioexception9) {
            }
        }
        if (datainputstream != null) {
            try {
                datainputstream.close();
            } catch (IOException ioexception10) {
            }
        }
        if (dataoutputstream != null) {
            try {
                dataoutputstream.close();
            } catch (IOException ioexception11) {
            }
        }
        if (httpconnection != null) {
            try {
                httpconnection.close();
            } catch (IOException ioexception12) {
            }
        }
        if (flag) {
            httpposterlistener.handleHttpError(s2);
            return false;
        } else {
            iNetErrorCnt = 0;
            httpposterlistener.receiveHttpResponse(s1);
            return true;
        }
    }

    public void abort() {
        aborting = true;
        synchronized (this) {
            notify();
        }
    }
}
